package com.tank.game.core;

import com.tank.game.api.Panel;
import com.tank.game.api.Shape;
import com.tank.game.api.*;
import com.tank.game.api.event.BulletEvent;
import com.tank.game.api.listener.Listener;

import java.awt.*;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;

/**
 * 缺省的坦克
 *
 * @author Zed
 */
public class DefaultTank extends DefaultBulletCollection implements Tank, Panel.ShowObject {

    private String id;

    private Range range;

    private Play play;

    public DefaultTank(Builder builder) {
        super(builder);
        this.id = Objects.requireNonNull(builder.id, "\"id\" cannot be null.");
        this.range = Objects.requireNonNull(builder.range, "\"range\" cannot be null.");
        this.play = Objects.requireNonNull(builder.play, "\"play\" cannot be null.");
        drawToPanel();
    }

    /**
     * 初始化构建
     *
     * @return {@link Builder}
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * 画出到画板
     */
    protected void drawToPanel() {
        LaunchPanel launchPanel = LaunchPanelUtils.LAUNCH_PANEL;
        launchPanel.draw(this, g -> {
            Position position = this.getRange().getPosition();
            g.fillRect(position.getX(), position.getX(), this.getWidth(), this.getHeight());
            g.setColor(Color.RED);
            g.fill3DRect(position.getX(), position.getY(), this.getWidth(), this.getHeight(), false);                  //左边轮子
            g.fill3DRect(position.getX() + 30, position.getY(), this.getWidth(), this.getHeight(), false);          //右边轮子
            g.fill3DRect(position.getX() + 10, position.getY() + 10, this.getWidth() * 2, (this.getHeight() / 2) + 10, false);    //盖子
            g.fillOval(position.getX() + 9, position.getY() + 18, this.getWidth() * 2, this.getHeight() / 3);               //炮塔
            g.drawLine(position.getX() + 19, position.getY() - 10, position.getY() + 19, position.getY() + 20);
        });
    }


    @Override
    public void close() {

    }

    @Override
    public void execMove(BiConsumer<Position, Tank> consumer) {
        Position position = this.getRange().getPosition();
        if (consumer != null) {
            consumer.accept(position, this);
            drawToPanel();
        }
    }

    @Override
    public boolean execMove(Position position) {
        return false;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public Play getPlay() {
        return this.play;
    }

    @Override
    public Range getRange() {
        return this.range;
    }

    @Override
    public boolean isHit(Bullet bullet) {
        if (Objects.equals(this.getRange().getPosition(), bullet.getRange().getPosition())) {
            this.close();
            return true;
        }
        return false;
    }

    @Override
    public double getSpeed() {
        return 5d;
    }

    @Override
    public Bullet fireBullet(Listener<BulletEvent> listener) {
        return null;
    }

    @Override
    public int getWidth() {
        return 10;
    }

    @Override
    public int getHeight() {
        return 60;
    }

    @Override
    public Position getPosition() {
        return this.getRange().getPosition();
    }

    @Override
    public Shape getObject() {
        return null;
    }

    public static class Builder extends DefaultBulletCollection.Builder {

        private List<Bullet> bullets;

        private String id;

        private Range range;

        private Play play;

        /**
         * 玩家
         *
         * @param play {@link Play}
         * @return {@link Builder}
         */
        public Builder play(Play play) {
            this.play = play;
            return this;
        }


        /**
         * 范围
         *
         * @param range {@link Range}
         * @return {@link Builder}
         */
        public Builder range(Range range) {
            this.range = range;
            return this;
        }

        /**
         * 编号
         *
         * @param id {@link List}
         * @return {@link Builder}
         */
        public Builder id(String id) {
            this.id = id;
            return this;
        }

        /**
         * 添加子弹集合
         *
         * @param bullets {@link java.util.List}
         * @return {@link DefaultBulletCollection.Builder}
         */
        @Override
        public Builder bullets(List<Bullet> bullets) {
            super.bullets(bullets);
            return this;
        }

        @Override
        public DefaultTank build() {
            return new DefaultTank(this);
        }
    }

}
